package com.zaimokuza.cn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.auth.Resources;
import com.zaimokuza.cn.entity.auth.RoleResourcesRelation;
import com.zaimokuza.cn.mapper.ResourcesMapper;
import com.zaimokuza.cn.service.ResourcesService;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @author zaimokuza
 */
@Service
public class ResourcesServiceImpl implements ResourcesService {

    @Resource
    private ResourcesMapper resourcesMapper;

    @Override
    public void addResources(Resources resources) {
        resourcesMapper.insertRecord(resources);
    }

    @Override
    public void updateResources(Resources resources) {
        resourcesMapper.updateRecord(resources);
    }

    @Override
    public void updateRoleResourcesRelations(RoleResourcesRelation roleResourcesRelation) {
        // 根据角色ID删除角色资源关联信息
        resourcesMapper.deleteBatchRoleResourcesRelationByRoleId(roleResourcesRelation.getRoleId());
        // 判断是否存在资源ID
        if (CollectionUtils.isEmpty(roleResourcesRelation.getResourcesIdList())) {
            return;
        }
        // 批量添加记录
        resourcesMapper.insertBatchRoleResourcesRelation(roleResourcesRelation);
    }

    @Override
    public void deleteResources(Long id) {
        // 删除资源
        resourcesMapper.deleteRecord(id);
        // 删除角色资源关联信息
        resourcesMapper.deleteBatchRoleResourcesRelationByResourcesId(id);
    }

    @Override
    public void deleteBatchRoleResourcesRelationByRoleId(String roleId) {
        resourcesMapper.deleteBatchRoleResourcesRelationByRoleId(roleId);
    }

    @Override
    public ArrayList<Resources> getResourcesListByUserId(String userId) {
        // 根据用户ID查询资源集合
        ArrayList<Resources> resourcesList = resourcesMapper.queryResourcesByUserId(userId);
        // 遍历节点集合，标记是否被选中，同时根据父节点整理
        HashMap<Long, ArrayList<Resources>> tempMap = new HashMap<>();
        for (Resources resources : resourcesList) {
            // 父节点ID
            Long parentId = resources.getParentId();
            // 根据父ID进行整理
            ArrayList<Resources> childList = tempMap.getOrDefault(parentId, new ArrayList<>());
            childList.add(resources);
            tempMap.putIfAbsent(parentId, childList);
        }
        // 递归整理树
        Resources rootNode = new Resources();
        rootNode.setId(0);
        recursionArrange(rootNode, tempMap);
        // 返回根节点的子节点集合
        return rootNode.getChildList();
    }

    @Override
    public ArrayList<Resources> getResourcesUrlList() {
        return resourcesMapper.queryResourcesUrlList();
    }

    /**
     * 递归整理树父子节点信息
     *
     * @param resources 需要整理子节点信息的资源
     * @param tempMap   记录资源父ID对应子节点集合的缓存Map集合
     */
    private void recursionArrange(Resources resources, HashMap<Long, ArrayList<Resources>> tempMap) {
        // 从缓存集合中获取对应的子节点集合
        ArrayList<Resources> childList = tempMap.get(resources.getId());
        // 不存在对应的子集合时跳过后续处理
        if (CollectionUtils.isEmpty(childList)) {
            return;
        }
        // 递归整理
        for (Resources child : childList) {
            recursionArrange(child, tempMap);
        }
        // 存放子节点集合
        resources.setChildList(childList);
    }

    @Override
    public ArrayList<Resources> getResourcesTree(Long ignoreId) {
        // 查询所有资源信息
        List<Resources> resourcesList = resourcesMapper.queryResourcesPage(null, null, null).getResult();
        // 遍历节点集合，标记是否被选中，同时根据父节点整理
        HashMap<Long, ArrayList<Resources>> tempMap = new HashMap<>();
        for (Resources resources : resourcesList) {
            // 忽略ID对应节点不处理
            if (ignoreId != null && resources.getId() == ignoreId) {
                continue;
            }
            // 父节点ID
            Long parentId = resources.getParentId();
            // 根据父ID进行整理
            ArrayList<Resources> childList = tempMap.getOrDefault(parentId, new ArrayList<>());
            childList.add(resources);
            tempMap.putIfAbsent(parentId, childList);
        }
        // 递归整理树
        Resources rootNode = new Resources();
        rootNode.setId(0);
        recursionArrange(rootNode, tempMap);
        // 返回根节点的子节点集合
        return rootNode.getChildList();
    }

    @Override
    public ArrayList<Long> getResourcesIdListByRoleId(String roleId) {
        return resourcesMapper.queryResourcesIdListByRoleId(roleId);
    }

    @Override
    public PageResult<Resources> getResourcesPage(String name, String url, Integer type, Integer page, Integer size) {
        // 设置分页信息
        PageHelper.startPage(page, size);
        // 查询
        Page<Resources> resources = resourcesMapper.queryResourcesPage(name, url, type);
        // 返回结果
        return PageResult.from(resources);
    }

    @Override
    public Resources getResources(Long id) {
        return resourcesMapper.queryResources(id);
    }

}
